package cn.sieis.tools;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

@Component
public class FastDFSClient {

	private static Logger log = LoggerFactory.getLogger(FastDFSClient.class);
	private static FastFileStorageClient fastFileStorageClient;
	private static FdfsWebServer fdfsWebServer;

	@Autowired
	public void setFastDFSClient(FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) {
		FastDFSClient.fastFileStorageClient = fastFileStorageClient;
		FastDFSClient.fdfsWebServer = fdfsWebServer;
	}

	/**
	 * @param multipartFile 文件对象
	 * @return 返回文件地址
	 * @author qbanxiaoli
	 * @description 上传文件
	 */
	public static String uploadFile(MultipartFile multipartFile) {
		try {
			StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
			return storePath.getFullPath();
		} catch (IOException e) {
			log.error(e.getMessage());
			return null;
		}

	}

	/**
	 * @param multipartFile 图片对象
	 * @return 返回图片地址
	 * @author qbanxiaoli
	 * @description 上传缩略图
	 */
	public static String uploadImageAndCrtThumbImage(MultipartFile multipartFile) {
		try {
			StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null);
			return storePath.getFullPath();
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}

	}

	/**
	 * @param file 文件对象
	 * @return 返回文件地址
	 * @author qbanxiaoli
	 * @description 上传文件
	 */
	public static String uploadFile(File file) {
		try {
			FileInputStream inputStream = new FileInputStream(file);
			StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
			return storePath.getFullPath();
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}

	}

	/**
	 * @param file 图片对象
	 * @return 返回图片地址
	 * @author qbanxiaoli
	 * @description 上传缩略图
	 */
	public static String uploadImageAndCrtThumbImage(File file) {
		try {
			FileInputStream inputStream = new FileInputStream(file);
			StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
			return storePath.getFullPath();
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}

	}

	/**
	 * @param bytes         byte数组
	 * @param fileExtension 文件扩展名
	 * @return 返回文件地址
	 * @author qbanxiaoli
	 * @description 将byte数组生成一个文件上传
	 */
	public static String uploadFile(byte[] bytes, String fileExtension) {
		ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
		StorePath storePath = fastFileStorageClient.uploadFile(stream, bytes.length, fileExtension, null);
		return storePath.getFullPath();

	}

	/**
	 * @param fileUrl 文件访问地址
	 * @param file    文件保存路径
	 * @author qbanxiaoli
	 * @description 下载文件
	 */
	public static boolean downloadFile(String fileUrl, File file) {
		try {
			StorePath storePath = StorePath.praseFromUrl(fileUrl);
			byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
			FileOutputStream stream = new FileOutputStream(file);
			stream.write(bytes);
			stream.close();
		} catch (Exception e) {
			log.error(e.getMessage());
			return false;
		}
		return true;

	}

	/**
	 * 前端页面下载
	 * 
	 * @param fileUrl
	 * @param file
	 * @return
	 */
	public static byte[] downloadFileWeb(String fileUrl, File file) {
		try {
			StorePath storePath = StorePath.praseFromUrl(fileUrl);
			byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
			return bytes;

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return null;
	}

	/**
	 * @param fileUrl 文件访问地址
	 * @author qbanxiaoli
	 * @description 删除文件
	 */
	public static boolean deleteFile(String fileUrl) {
		if (StringUtils.isEmpty(fileUrl)) {
			return false;
		}
		try {
			StorePath storePath = StorePath.praseFromUrl(fileUrl);
			fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
		} catch (Exception e) {
			log.error(e.getMessage());
			return false;
		}
		return true;

	}

	// 封装文件完整URL地址
	public static String getResAccessUrl(String path) {
		String url = fdfsWebServer.getWebServerUrl() + path;
		log.info("上传文件地址为：\n" + url);
		return url;
	}

}
